from django.db import models
from django.urls import reverse


# Create your models here.

class Mood(models.Model):
    """ 项目表 """
    Is_Public_CHOICES = (
        (1, "公开"),
        (2, "好友圈"),
        (3, "仅我可见"),
        (4, "粉丝"),
    )

    intro = models.TextField(verbose_name='介绍', max_length=150)
    is_public = models.SmallIntegerField(verbose_name='是否公开', choices=Is_Public_CHOICES, default=1)
    author = models.ForeignKey(verbose_name='发布人', to='account.UserInfo', on_delete=models.PROTECT,
                               related_name='author')  # 用户被删除，相关的内容需要被删除吗？
    is_delete = models.BooleanField(verbose_name='是否删除', default=False)
    likers = models.ManyToManyField('account.UserInfo', through='MoodLiker', related_name='likers')
    like_amount = models.PositiveIntegerField(verbose_name='点赞数', default=10000, null=True, blank=True)
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def get_user(self):
        return self.author

    @property  # 装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用，这样可以防止属性被修改。
    def get_images(self):
        return self.images.all()  # ForeignKey related_name

    # 获得当前点赞的用户列表。
    @property
    def get_likers(self):
        return self.mood_likers.all()

    # 获取心情地址
    # def get_absolute_url(self):
    #     return reverse('mood_detail', args=[self.pk])

    def get_url(self):
        return reverse('mood_detail', kwargs={'mood_id': self.pk})

class MoodImage(models.Model):
    mood = models.ForeignKey(verbose_name='心情', to='Mood', on_delete=models.CASCADE, null=True, blank=True,
                             related_name='images')  # 删除心情时同时删除关联的图片?
    file_url = models.CharField(verbose_name='图片地址', max_length=255)  # https://桶.cos.ap-chengdu/....
    thumbnail_url = models.CharField(verbose_name='图片地址', max_length=255)
    key = models.CharField(verbose_name='文件储存在COS中的KEY', max_length=128, null=True, blank=True)
    # int类型最大表示的数据
    size = models.BigIntegerField(verbose_name='文件大小', null=True, blank=True, help_text='字节')
    author = models.ForeignKey(verbose_name='发布人', to='account.UserInfo', on_delete=models.PROTECT)
    create_datetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    # 图片大小
    # 图片格式
    # 缩略图


class MoodLiker(models.Model):
    mood = models.ForeignKey(verbose_name='点赞心情', to='Mood', on_delete=models.CASCADE,
                             related_name='mood_likers')  # ForeignKey 这里应该用多对多的关系。
    user = models.ForeignKey(verbose_name='用户', to='account.UserInfo', on_delete=models.CASCADE,
                             related_name='liker_obj')
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
